package com.mytijian.openapi.adapter.alihalt.service.impl;

import java.util.Date;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mytijian.code.param.alihalt.ToTBAvailableParam;
import com.mytijian.code.service.MerchantService;
import com.mytijian.openapi.adapter.alihalt.dao.McodeTokenRelDAO;
import com.mytijian.openapi.adapter.alihalt.exception.AlihaltException;
import com.mytijian.openapi.adapter.alihalt.model.McodeTokenRel;
import com.mytijian.openapi.adapter.alihalt.param.AlihaltMcodeCheckParam;
import com.mytijian.openapi.adapter.alihalt.result.AliGenResult;
import com.mytijian.openapi.adapter.alihalt.result.AlihallMcodeToken;
import com.mytijian.openapi.adapter.alihalt.service.AlihaltMcodeCheckService;

@Service
public class AlihaltMcodeCheckServiceImpl implements AlihaltMcodeCheckService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private McodeTokenRelDAO mcodeTokenRelDAO;
	
	@Autowired
	private MerchantService merchantService;

	@Override
	public AliGenResult<AlihallMcodeToken> checkMcode(AlihaltMcodeCheckParam param) {
		
		log.info("[阿里健康] [电子码校验] 传入参数 {}", JSON.toJSONString(param));
		
		checkMcodeIsAvailable(param);

		McodeTokenRel mcodeTokenRel = buildMcodeTokenRel(param.getServiceNumber());

		AliGenResult<AlihallMcodeToken> result = buildAliResult(mcodeTokenRel);

		log.info("[阿里健康] [电子码校验] 执行结果 {}", JSON.toJSONString(result) );
		
		return result;
	}

	private void checkMcodeIsAvailable(AlihaltMcodeCheckParam param) {
		String mcode = param.getServiceNumber();
		
		ToTBAvailableParam request = new ToTBAvailableParam();
		request.setCode(mcode);
		
		try {
			merchantService.available(request);
		} catch (Exception e) {
			log.warn("*** [阿里健康] 电子码校验无效 {}", param.getServiceNumber());
			throw new AlihaltException("400", "电子码无效");
		}
	}

	private McodeTokenRel buildMcodeTokenRel(String mcode) {
		McodeTokenRel rel = mcodeTokenRelDAO.getMcodeTokenRel(mcode);
		if (rel == null) {
			rel = new McodeTokenRel();
			rel.setMcode(mcode);
			updateTokenField(rel);
			mcodeTokenRelDAO.insertMcodeTokenRel(rel);
		} else {
			updateTokenField(rel);
			mcodeTokenRelDAO.updateMcodeTokenRel(rel);
		}
		rel = mcodeTokenRelDAO.getMcodeTokenRel(mcode);
		return rel;
	}

	private AliGenResult<AlihallMcodeToken> buildAliResult(McodeTokenRel rel) {
		AlihallMcodeToken mr = new AlihallMcodeToken();
		mr.setToken(rel.getToken());
		AliGenResult<AlihallMcodeToken> result = new AliGenResult<>();
		result.setResponseCode("200");
		result.setResponseMessage("success");
		result.setResult(mr);
		return result;
	}

	private void updateTokenField(McodeTokenRel rel) {
		rel.setExpires(new Date(System.currentTimeMillis()+(7200*1000L)));
		StringBuilder token = new StringBuilder();
		token.append(UUID.randomUUID().toString().replace("-",""));
		token.append(UUID.randomUUID().toString().replace("-",""));
		rel.setToken(token.toString());
	}

}
